package cn.framelife.rainbow.util;

import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import cn.framelife.rainbow.constant.Constant;
import cn.framelife.rainbow.constant.JRTemplate;
import cn.framelife.rainbow.db.entity.ClazzProperty;
import cn.framelife.rainbow.db.entity.PanelProperty;
import cn.framelife.rainbow.db.entity.SpringProperty;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;

/**
 * Freemarker工具类
 *
 * @ClassName: FreemarkerUtil 
 * @author 潘广伟  p_3er@qq.com 
 * @date 2013年11月22日 下午2:41:43 
 *
 */
public class FreemarkerUtil {
	
	/**
	 * 创建配置
	 *
	 * @return
	 * @throws Exception
	 */
	private static Configuration createConfiguration() throws Exception {
		Configuration cfg = new Configuration();
		cfg.setEncoding(Locale.CHINA, Constant.ECODING);
		cfg.setDirectoryForTemplateLoading(FileUtil.getFile("template"));
		cfg.setObjectWrapper(new DefaultObjectWrapper());
		return cfg;
	}
	
	private static Template getTemplate(String fileName) throws Exception{
		return createConfiguration().getTemplate(StringUtil.replaceSeparator(fileName));
	}
	
	/**
	 * 
	 * 创建Spring配置文件
	 *
	 * @param  springProperty    设定文件 
	 * @return void    返回类型 
	 * @throws Exception 
	 */
	public static void createSpringConfig(SpringProperty springProperty) throws Exception{
		Template temp = getTemplate(JRTemplate.SPRING);
		Map<String, Object> root = new HashMap<String, Object>();
		root.put("spring", springProperty);
		
		createFile(temp, root, springProperty.getPath());
	}
	
	/**
	 * 创建web.xml配件文件
	 *
	 * @param path
	 * @param actionPackages
	 * @throws Exception
	 */
	public static void createWebConfig(String path,String actionPackages) throws Exception{
		Template temp = getTemplate(JRTemplate.WEB);
		Map<String, Object> root = new HashMap<String, Object>();
		root.put("actionPackages", actionPackages);
		
		createFile(temp, root, path);	
	}

	
	/**
	 * 创建页面Extjs代码文件
	 *
	 * @param panelProperty
	 * @throws Exception
	 */
	public static void createPanel(PanelProperty panelProperty) throws Exception{
		Template temp = getTemplate(JRTemplate.PAGE);
		Map<String, Object> root = new HashMap<String, Object>();
		root.put("panel", panelProperty);
		String targetPath = StringUtil.merge(FileUtil.getWebRootName(panelProperty.getProjectName()),"/js/pages/",panelProperty.getModuleName(),"/");
		FileUtil.createPath(targetPath);
		createFile(temp, root,StringUtil.merge(targetPath,panelProperty.getEntityName(),"Panel.js"));

	}
	
	/**
	 * 创建一个实体类
	 *
	 * @param clazzProperty
	 * @throws Exception
	 */
	public static void createEntity(ClazzProperty clazzProperty) throws Exception {
		createClass(clazzProperty,JRTemplate.ENTITY);
	}
	
	
	/**
	 * 创建BaseDao
	 *
	 * @param clazzProperty
	 * @throws Exception
	 */
	public static void createIBaseDao(ClazzProperty clazzProperty) throws Exception{
		createClass(clazzProperty,JRTemplate.IBASEDAO);
	}
	
	/**
	 * 创建DaoBaseImpl
	 *
	 * @param clazzProperty
	 * @throws Exception
	 */
	public static void createBaseDao(ClazzProperty clazzProperty) throws  Exception{
		createClass(clazzProperty,JRTemplate.BASEDAO);
	}
	
	/**
	 * 创建实体Dao接口
	 *
	 * @param clazzProperty
	 * @throws Exception
	 */
	public static void createEntityIDao(ClazzProperty clazzProperty) throws  Exception{
		createClass(clazzProperty,JRTemplate.IDAO);
	}
	
	/**
	 * 创建实体Dao
	 *
	 * @param clazzProperty
	 * @throws Exception
	 */
	public static void createEntityDao(ClazzProperty clazzProperty) throws  Exception{
		createClass(clazzProperty,JRTemplate.DAO);
	}
	
	/**
	 * 创建实体IService
	 *
	 * @param clazzProperty
	 * @throws Exception
	 */
	public static void createIService(ClazzProperty clazzProperty) throws  Exception{
		createClass(clazzProperty,JRTemplate.ISERVICE);
	}
	
	/**
	 * 创建实体Service
	 *
	 * @param clazzProperty
	 * @throws Exception
	 */
	public static void createService(ClazzProperty clazzProperty) throws  Exception{
		createClass(clazzProperty,JRTemplate.SERVICE);
	}
	
	/**
	 * 创建实体Action
	 *
	 * @param clazzProperty
	 * @throws Exception
	 */
	public static void createAction(ClazzProperty clazzProperty) throws  Exception{
		createClass(clazzProperty,JRTemplate.ACTION);
	}
	
	/**
	 * 创建MenuAction
	 *
	 * @param clazzProperty
	 * @throws Exception
	 */
	public static void createMenuAction(String projectName,String packageURL) throws Exception{
		ClazzProperty clazzProperty = new ClazzProperty();
		clazzProperty.setProjectName(projectName);
		clazzProperty.setClassName("MenuAction");
		clazzProperty.setPackageURL(packageURL);
		
		createClass(clazzProperty,JRTemplate.MENUACTION);
	}
	
	/**
	 * 创建java类
	 *
	 * @param clazzProperty
	 * @param template
	 * @throws Exception
	 */
	private static void createClass(ClazzProperty clazzProperty,String template) throws Exception{
		String srcPath = FileUtil.getPackagePath(clazzProperty.getProjectName(), clazzProperty.getPackageURL());
		createPackage(srcPath);
		Template temp = getTemplate(template);
		Map<String, Object> root = new HashMap<String, Object>();
		root.put("class", clazzProperty);
		String classPath =StringUtil.merge(srcPath,clazzProperty.getClassName(),".java");
		createFile(temp, root, classPath);
	}
	
	
	
	/**
	 * 生成一个文件
	 *
	 * @param temp
	 * @param root
	 * @param filePath
	 * @throws Exception
	 */
	private static void createFile(Template temp,Map<String, Object> root,String filePath) throws Exception{
		Writer out = new OutputStreamWriter(new FileOutputStream(filePath),Constant.ECODING);
		temp.process(root, out);
		out.flush();
		out.close();
	}
	
	
	/**
	 * 创建包
	 *
	 * @Title: createPackage 
	 * @param  projectName
	 * @param  packageURL  
	 * @return void  
	 * @throws
	 */
	private static void createPackage(String packagePath){
		FileUtil.createPath(packagePath);
	}

	
}
